/**
 *
 */
package com.cloudspace.jindun.user;


import android.net.Uri;
import android.text.TextUtils;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.activity.RegisterActivity;
import com.cloudspace.jindun.constants.Constants;
import com.cloudspace.jindun.dao.LoginUser;
import com.cloudspace.jindun.log.APPLog;
import com.cloudspace.jindun.manager.UserManager;
import com.cloudspace.jindun.network.IntenetUtil;
import com.cloudspace.jindun.network.NetworkMonitor;
import com.cloudspace.jindun.utils.AppPreferences;
import com.cloudspace.jindun.utils.ObjUtil;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * 存储用户的一些状态
 */
public class UserSessionManager {
    private static final String TAG = UserSessionManager.class.getSimpleName();

    private UserSessionManager() {
    }

    private static UserSessionManager instance;

    private boolean isSilent; //对讲静默状态

    public String getSipDomain() {
        LoginUser user = getCurrentLoginUser();

        if (ObjUtil.isEmpty(user)) {
            loginOut();
            return "";
        }

        String account = user.getSipAccout();
        if (TextUtils.isEmpty(account) || !account.contains("@")) {
            return "";
        }

        return account.split("@")[1];
    }

    public String getSipUserName() {
        LoginUser user = getCurrentLoginUser();

        if (ObjUtil.isEmpty(user)) {
            loginOut();
            return "";
        }

        String account = user.getSipAccout();
        if (TextUtils.isEmpty(account) || !account.contains("@")) {
            return "";
        }

        return account.split("@")[0];
    }

    public String getSipPassword() {
        LoginUser user = getCurrentLoginUser();

        if (ObjUtil.isEmpty(user)) {
            loginOut();
            return "";
        }

        return user.getSipPassword();
    }

    public String getUserId() {
        LoginUser user = getCurrentLoginUser();
        if (ObjUtil.isEmpty(user)) {
            loginOut();
            return "";
        }

        return user.getUserId();

    }

    public boolean isSilent() {
        return isSilent;
    }

    public void setSilent(boolean silent) {
        isSilent = silent;
    }

    public static synchronized UserSessionManager getInstance() {
        if (instance == null) {
            instance = new UserSessionManager();
        }

        return instance;
    }

    public String getSipAccount() {
        LoginUser user = getCurrentLoginUser();
        if (ObjUtil.isEmpty(user)) {
            loginOut();
            return "";
        }

        return user.getSipAccout();
    }

    public void setRongyunUserInfo(LoginUser user) {
        if (ObjUtil.isEmpty(user)) {
            return;
        }

        UserInfo rongyunInfo = new UserInfo(user.getUserId(),
                user.getUserName(), Uri.parse(user.getIconUrl()));
        RongIM.getInstance().setCurrentUserInfo(rongyunInfo);
    }

    public boolean isInterphoneAllowed() {
        return AppPreferences.getBoolean(Constants.INTERPHONE_SWITCH, true);
    }


    public static LoginUser getCurrentLoginUser() {
        return UserManager.getInstance().queryLoginUser();
    }

    public static boolean yyCanUse() {
        if (!NetworkMonitor.isNetworkAvailable()) {
            return false;
        }

        if (NetworkMonitor.isWifi()) {
            return AppPreferences.getBoolean(Constants.YY_WIFI_SWITCH, true);
        }

        if (IntenetUtil.is2G3GMode()) {
            return AppPreferences.getBoolean(Constants.YY_2G_SWITCH, true);
        }

        if (IntenetUtil.is4GMode()) {
            return AppPreferences.getBoolean(Constants.YY_4G_SWITCH, true);
        }

        return true;
    }

    public static boolean contactUpdate() {
        if (!NetworkMonitor.isNetworkAvailable()) {
            return false;
        }

        if (NetworkMonitor.isWifi()) {
            return AppPreferences.getBoolean(Constants.PHONE_UPDATE_WIFI_SWITCH, true);
        }

        if (IntenetUtil.is2G3GMode()) {
            return AppPreferences.getBoolean(Constants.PHONE_UPDATE_2G_SWITCH, true);
        }

        if (IntenetUtil.is4GMode()) {
            return AppPreferences.getBoolean(Constants.PHONE_UPDATE_4G_SWITCH, true);
        }

        return true;
    }

    public static boolean isVibrateOn() {
        return AppPreferences.getBoolean(Constants.VIBRATE_SWITCH, false);
    }

    public static void updateUserInfo(LoginUser result) {
        if (result != null && result != null) {
            APPLog.zxhLog(TAG + " updateUserInfo begin");
            UserManager.updateUserInfo(result);
            APPLog.zxhLog(TAG + " updateUserInfo end");
        }
    }

    public void loginOut() {
        UserManager.getInstance().deleteAll();
        //融云退出
        RongIM.getInstance().logout();
        RegisterActivity.showActivity(JindunApp.getInstance());
    }

}
